<?php 
class filesAction extends AdminMasterUploadControl{
	private $finder;
	public function execute($request){
		//checking authentication
		$directory_path  	= $request->getParameter('directory_path','');
		$filter				= $request->getParameter('filter','');
		if($directory_path=='all') $directory_path = '';
		$allow = explode('|',sfConfig::get('app_uploadControl_allowed'));
		if($filter=='' || $filter=='*'){
			$filter = str_replace('|', ',', sfConfig::get('app_uploadControl_allowed'));
			//echo $filter;die;
		}else{
			$filter_arr = explode('|',$filter);
			$sepa ='';
			$filter = '';
			foreach($filter_arr as $val){
				if(strpos(sfConfig::get('app_uploadControl_allowed'), strtolower($val))!==false){
					$filter.=$sepa.$val;
					$sepa = ',';
				}
			}
		}
		$jsonReturn = array(
				'path'		=> '/uploads/'.$directory_path,
				'has_file' 	=> 0,
				'file_list' => array()
			);
		//if(!empty($directory_path)){
			$this->finder = new sfFinder();	
			$this->finder->maxdepth(0);
			$this->finder->setType('file');
			$this->finder->name('*\.{'.$filter.','.strtoupper($filter).'}');
			$this->finder->name();
			$file_list = $this->finder->in(sfConfig::get('sf_upload_dir').'/'.$directory_path);
			if(count($file_list)){
				$jsonReturn['has_file'] = count($file_list);
				foreach($file_list as $dir){
					$arr = explode(sfConfig::get('app_uploadControl_path'), $dir);
					if(count($arr)<2) continue;
					$jsonReturn['file_list'][] = basename($arr[1]);
				}
			}
		//} 
		print_r(json_encode($jsonReturn));
		return sfView::NONE;
	}
}